<!DOCTYPE html>
<head>
    <title>参与答题</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <meta http-equiv="Cache-Control" content="no-store,max-age=0,no-cache" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
    <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/2.4.1/weui.min.css" />
	<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
</head>
<body>
<div id="wechat-web-container" style="width: 100%;height: 100vh;display: flex;flex-direction: column;align-items: center;justify-content: center;">
    
</div>
<script type="text/javascript">
var is_wxApi_authorize = false;
$(document).ready(function() {
	$.ajax({
		url: 'http://newsales.chinaesf.net/yl/wechat/getGpsLocation',
		success: function(res){
			console.log("getJSSdk=", res);
			if (res.code === 1000) {
				var config = res.data;
				wx.config({
					debug: false,
					appId: config.appId,
					timestamp: config.timestamp,
					nonceStr: config.nonceStr,
					signature: config.signature,
					jsApiList: [
						'checkJsApi',
						'openLocation',
						'getLocation'
					]
				});
				init();
			} else {
				alert("请求失败，请稍后再试！");
			}
		},
		error: function (err) {
			alert("网络繁忙，请稍后再试！");
		}
	})
});
// 
function init() {
	wx.ready(function () {
		wx.checkJsApi({
			jsApiList: [
				'getLocation'
			],
			success: function (res) {
				// alert(JSON.stringify(res));
				// alert(JSON.stringify(res.checkResult.getLocation));
				if (res.checkResult.getLocation == false) {
					alert('你的微信版本太低，不支持微信JS接口，请升级到最新的微信版本！');
					var str = '<div style="font-size: 14px;color: #000;padding: 15px 0;text-align: center;">你的微信版本太低，不支持微信JS接口，请升级到最新的微信版本！</div>';
					$("#wechat-web-container").html(str);
					return;
				} else {
					// 获取用户当前坐标
					wx.getLocation({
						success: function (res) {
							console.log("getLocation=", res);
							var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
							var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
							var speed = res.speed; // 速度，以米/每秒计
							var accuracy = res.accuracy; // 位置精度
							
							is_wxApi_authorize = true;
							// 根据用户当前坐标逆地址解析详细地址信息
							getLocationMsg(latitude, longitude);
						},
						cancel: function (res) {
							alert('请先授权获取地理位置才能参与答题！');
							is_wxApi_authorize = false;
							var str = '<div style="font-size: 14px;color: #000;padding: 15px 0;text-align: center;">请先授权获取地理位置才能参与答题！</div>';
							var str = '<button id="reauthorization" style="font-size: 20px;background: #00BC0C;color: #fff;border: 0;border-radius: 5px;height: 45px;line-height: 45px;text-align: center;">重新授权</button>';
							
							$("#wechat-web-container").html(str);
							
							$("#reauthorization").on("click", init());
						}
					});
				}
			},
			fail: function () {
				alert("fail");
			}
		});
	});
}

// 获取用户详细地址信息
function getLocationMsg(lat, lng) {
	$.ajax({
		url: 'https://apis.map.qq.com/ws/geocoder/v1/',
		method:'get',
		data: {
			location: lat+','+lng,
			key: '7OBBZ-EQLK4-GO3U2-D34KX-EBCCS-XLBMB',
			output: 'jsonp'
		},
		dataType: 'jsonp',
		async: false,
		success: function(res){
			console.log("getLocationMsg=", res);
			var str = '';
			var message = res.message;
			if (message.indexOf('ok') >= 0) {
				var result = res.result;
				if (result.address_component.city == '南宁市') {
					str += '<a href="https://ks.youkaoshi.cn/doexam/2PgB2dV2gA.html" id="toWxapp" style="font-size: 16px;background: #00BC0C;color: #fff;border: 1px solid #00BC0C;border-radius: 4px;height: 45px;line-height: 45px;text-align: center;">参与答题</a>';
				} else{
					str += '<span style="font-size: 16px;color: #999;line-height: 1.6;">您当前所在位置不属于南宁，暂无法参与答题~</span>'
				}
			}
			
			
			$("#wechat-web-container").html(str);
		},
		error:function(err){
			console.log("getLocationMsg_err=", err);
		}					
	});
}
</script>
</body>
</html>